package com.shrcl.engineerprocess.domain;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.shrcl.engineerprocess.domain.CostEstimationDAO;
import com.shrcl.engineerprocess.domain.CostEstimationProcesss;
import com.shrcl.engineerprocess.domain.EngineerCostEstimation;
import com.shrcl.engineerprocess.domain.EngineerCostEstimationStatus;
import com.shrcl.engineerprocess.domain.EngineerDAO;
import com.shrcl.engineerprocess.domain.EngineerProcessTaskExtension;
import com.shrcl.engineerprocess.domain.TSCProcessRole;
import com.shrcl.engineerprocess.domain.TSCProcessRoleDAO;
import com.shrcl.engineerprocess.domain.organization.EngineerTechnicalLeaderRoleProvider;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCEmployeeDAO;
import com.shrct.businessprocess.organization.RCOrganizationService;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.security.User;
import edu.fudan.langlab.domain.task.TSKTask;
import edu.fudan.langlab.domain.task.TSKTaskCriteria;
import edu.fudan.langlab.domain.task.TSKTaskExtension;
import edu.fudan.langlab.domain.task.TSKTaskOwner;
import edu.fudan.langlab.domain.task.TSKUserOwner;
import edu.fudan.langlab.domain.task.impl.TSKUserOwnerImpl;
import edu.fudan.langlab.domain.workflow.BusinessProcess;
import edu.fudan.langlab.domain.workflow.ProcessManager;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.test.BaseTest;
import java.util.Collection;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(locations = "classpath:HibernateApplicationContext-test.xml")
public class EngineerCostEstProcessTest extends BaseTest {
  public EngineerCostEstProcessTest() {
    super();
  }
  
  @Autowired
  private RCOrganizationService rcOrganizationService;
  
  @Autowired
  private EngineerTechnicalLeaderRoleProvider engineerTechnicalLeaderRoleProvider;
  
  @Autowired
  private ISystemUserService systemUserService;
  
  @Autowired
  private EngineerDAO engineerDAO;
  
  @Autowired
  private CostEstimationDAO costEstimationDAO;
  
  @Autowired
  private ProcessManager processManager;
  
  @Autowired
  private RCEmployeeDAO rcEmployeeDAO;
  
  @Autowired
  private TSCProcessRoleDAO processRoleDAO;
  
  @Autowired
  private RCOrganizationService organizationService;
  
  public EngineerProcessTaskExtension getEngineerProcessTaskExtension4(final TSKTaskOwner owner) {TSKTaskCriteria tSKTaskCriteria=getObjectFactory().createQueryCriteria(TSKTaskCriteria.class);
    
    TSKTaskCriteria _setOwner = tSKTaskCriteria.setOwner(owner);
    Collection<TSKTask> _list = _setOwner.list();
    final Function1<TSKTask,Boolean> _function = new Function1<TSKTask,Boolean>() {
        public Boolean apply(final TSKTask it) {
          Boolean _isFinished = it.isFinished();
          boolean _not = (!_isFinished);
          return Boolean.valueOf(_not);
        }
      };
    Iterable<TSKTask> _filter = IterableExtensions.<TSKTask>filter(_list, _function);
    final Function1<TSKTask,TSKTaskExtension> _function_1 = new Function1<TSKTask,TSKTaskExtension>() {
        public TSKTaskExtension apply(final TSKTask it) {
          TSKTaskExtension _extension = it.getExtension();
          return _extension;
        }
      };
    Iterable<TSKTaskExtension> _map = IterableExtensions.<TSKTask, TSKTaskExtension>map(_filter, _function_1);
    Iterable<EngineerProcessTaskExtension> _filter_1 = Iterables.<EngineerProcessTaskExtension>filter(_map, EngineerProcessTaskExtension.class);
    EngineerProcessTaskExtension _head = IterableExtensions.<EngineerProcessTaskExtension>head(_filter_1);
    return _head;
  }
  
  @Test
  public void initProcess() {
    InputOutput.<String>println("================>EngineerCostEstProcessTest start");
    final RCEmployee salesMan = this.organizationService.registerEmployee("1111111");
    final RCEmployee techManager = this.organizationService.registerEmployee("2222222");
    final RCEmployee techDirector = this.organizationService.registerEmployee("3333333");
    Assert.assertNotNull(salesMan);
    User _asUser = salesMan.asUser();
    Assert.assertNotNull(_asUser);
    User _asUser_1 = salesMan.asUser();
    this.systemUserService.setCurrentUser(_asUser_1);
    Assert.assertNotNull(techDirector);
    User _asUser_2 = techDirector.asUser();
    Assert.assertNotNull(_asUser_2);
    Assert.assertNotNull(techManager);
    User _asUser_3 = techManager.asUser();
    Assert.assertNotNull(_asUser_3);
    TSCProcessRole _get = this.processRoleDAO.get();
    _get.setFirstLevelVerifier(techManager);
    TSCProcessRole _get_1 = this.processRoleDAO.get();
    _get_1.setSecondLevelVerifier(techDirector);
    InputOutput.<String>println("==================>All roles are ready==========");
    final Procedure1<EngineerCostEstimation> _function = new Procedure1<EngineerCostEstimation>() {
        public void apply(final EngineerCostEstimation it) {
        }
      };
    final EngineerCostEstimation est = this.costEstimationDAO.create(_function);
    est.submit();
    EngineerCostEstimationStatus _status = est.getStatus();
    String _name = _status.name();
    boolean _equals = Objects.equal(_name, "estimating");
    Assert.assertTrue(_equals);
    User _asUser_4 = techManager.asUser();
    IGenericQuery<TSKUserOwner> query=getObjectFactory().createGenericQuery(TSKUserOwner.class,"select e from edu.fudan.langlab.domain.task.impl.TSKUserOwnerImpl as e   where ((e.user) = (:_asUser_4)) and  e.active = 1      ").setParameter("_asUser_4",_asUser_4);
    TSKUserOwner teu = query.uniqueResult();
    boolean _equals_1 = Objects.equal(teu, null);
    if (_equals_1) {
      User _asUser_5 = techManager.asUser();TSKUserOwner tSKUserOwner=TSKUserOwnerImpl.create(_asUser_5,getObjectFactory());
      teu = tSKUserOwner;
    }
    Assert.assertNotNull(teu);
    EngineerProcessTaskExtension _engineerProcessTaskExtension4 = this.getEngineerProcessTaskExtension4(teu);
    final Procedure1<EngineerProcessTaskExtension> _function_1 = new Procedure1<EngineerProcessTaskExtension>() {
        public void apply(final EngineerProcessTaskExtension verifyTask) {
          Assert.assertNotNull(verifyTask);
          BusinessProcess _businessProcess = verifyTask.getBusinessProcess();
          final CostEstimationProcesss p = ((CostEstimationProcesss) _businessProcess);
          Assert.assertNotNull(p);
          p.TechnicalManagerEst_reject("modify");
        }
      };
    ObjectExtensions.<EngineerProcessTaskExtension>operator_doubleArrow(_engineerProcessTaskExtension4, _function_1);
    User _asUser_6 = salesMan.asUser();
    IGenericQuery<TSKUserOwner> query_1=getObjectFactory().createGenericQuery(TSKUserOwner.class,"select e from edu.fudan.langlab.domain.task.impl.TSKUserOwnerImpl as e   where ((e.user) = (:_asUser_6)) and  e.active = 1      ").setParameter("_asUser_6",_asUser_6);
    TSKUserOwner susr = query_1.uniqueResult();
    boolean _equals_2 = Objects.equal(susr, null);
    if (_equals_2) {
      User _asUser_7 = salesMan.asUser();TSKUserOwner tSKUserOwner_1=TSKUserOwnerImpl.create(_asUser_7,getObjectFactory());
      susr = tSKUserOwner_1;
    }
    Assert.assertNotNull(susr);
    EngineerProcessTaskExtension _engineerProcessTaskExtension4_1 = this.getEngineerProcessTaskExtension4(susr);
    final Procedure1<EngineerProcessTaskExtension> _function_2 = new Procedure1<EngineerProcessTaskExtension>() {
        public void apply(final EngineerProcessTaskExtension verifyTask) {
          Assert.assertNotNull(verifyTask);
          BusinessProcess _businessProcess = verifyTask.getBusinessProcess();
          final CostEstimationProcesss p = ((CostEstimationProcesss) _businessProcess);
          Assert.assertNotNull(p);
          p.BusinessManModify();
        }
      };
    ObjectExtensions.<EngineerProcessTaskExtension>operator_doubleArrow(_engineerProcessTaskExtension4_1, _function_2);
    Assert.assertNotNull(teu);
    EngineerProcessTaskExtension _engineerProcessTaskExtension4_2 = this.getEngineerProcessTaskExtension4(teu);
    final Procedure1<EngineerProcessTaskExtension> _function_3 = new Procedure1<EngineerProcessTaskExtension>() {
        public void apply(final EngineerProcessTaskExtension verifyTask) {
          Assert.assertNotNull(verifyTask);
          BusinessProcess _businessProcess = verifyTask.getBusinessProcess();
          final CostEstimationProcesss p = ((CostEstimationProcesss) _businessProcess);
          Assert.assertNotNull(p);
          p.TechnicalManagerEst_accept("good");
        }
      };
    ObjectExtensions.<EngineerProcessTaskExtension>operator_doubleArrow(_engineerProcessTaskExtension4_2, _function_3);
    User _asUser_8 = techDirector.asUser();
    IGenericQuery<TSKUserOwner> query_2=getObjectFactory().createGenericQuery(TSKUserOwner.class,"select e from edu.fudan.langlab.domain.task.impl.TSKUserOwnerImpl as e   where ((e.user) = (:_asUser_8)) and  e.active = 1      ").setParameter("_asUser_8",_asUser_8);
    TSKUserOwner ted = query_2.uniqueResult();
    boolean _equals_3 = Objects.equal(ted, null);
    if (_equals_3) {
      User _asUser_9 = techDirector.asUser();TSKUserOwner tSKUserOwner_2=TSKUserOwnerImpl.create(_asUser_9,getObjectFactory());
      ted = tSKUserOwner_2;
    }
    Assert.assertNotNull(ted);
    EngineerProcessTaskExtension _engineerProcessTaskExtension4_3 = this.getEngineerProcessTaskExtension4(ted);
    final Procedure1<EngineerProcessTaskExtension> _function_4 = new Procedure1<EngineerProcessTaskExtension>() {
        public void apply(final EngineerProcessTaskExtension verifyTask) {
          Assert.assertNotNull(verifyTask);
          BusinessProcess _businessProcess = verifyTask.getBusinessProcess();
          final CostEstimationProcesss p2 = ((CostEstimationProcesss) _businessProcess);
          Assert.assertNotNull(p2);
          p2.TechnicalDirectorConfirm_accept("OK");
        }
      };
    ObjectExtensions.<EngineerProcessTaskExtension>operator_doubleArrow(_engineerProcessTaskExtension4_3, _function_4);
  }
}
